import net
import transaction
import system

# 常量
ON_CHAIN_COST = 5 # 上链的时间开销
OFF_CHAIN_COST = 1 # 链下的时间开销
SHARD_TPS = 10 # 每个分片的TPS
ch_tps = 10 # 每条通道的交易处理能力

# 变量的初始化
# 分片区块链相关
shardNum = 16 # 分片的数量
# pcn相关
nodeNum = 1600 # 节点数量
steps = 3 # 通道路径的最长步长
poss = 0.5 # pcn的重连概率，WS算法需要
ch_life = 50 # 通道寿命
chDensity = 20

# 交易相关
patchNum = 320000 #320000 # 需要处理的交易总数
txs = transaction.generate_patch_tx(nodeNum, patchNum)
print("交易获取, 数量=", len(txs))
shardedTx = transaction.transaction_shard(txs, shardNum)
print("交易分片成功")

# 默认通道网络
# dpcn = net.ws_pcn(nodeNum, chDensity, poss)
# dpath = net.pcn_path(dpcn, steps)

# 通道相关
pcn_num = 15
paths = []
for i in range(pcn_num):
    chDensity = 5 + i*5
    pcn = net.ws_pcn(nodeNum, chDensity, poss)
    path = net.pcn_path(pcn, steps)
    print("pcn", i, " 创建成功")
    paths.append(path)


# 进行处理过程仿真
print("模拟交易处理")
final = []
pcn_tps = (ch_tps * nodeNum * chDensity)/2 # pcn网络的交易处理能力
t = system.tx_exec(node_num=nodeNum, snum=shardNum, txs=shardedTx, ledger_tps=SHARD_TPS, path=paths[0], pcn_tps=pcn_tps, ch_life=ch_life, isAllow=0)
final.append(t)


for i in range(pcn_num):
    t = system.tx_exec(node_num=nodeNum, snum=shardNum, txs=shardedTx, ledger_tps=SHARD_TPS, path=paths[i], pcn_tps=pcn_tps, ch_life=ch_life, isAllow=1)
    final.append(t)

# 输出处理时间
print('最终交易处理时间 = ', final)
'''
'''

# net = net.Net(100)
# net.display()